package com.exit66.jukebox.servlet.xml;

import javax.servlet.http.HttpServletRequest;

import com.exit66.jukebox.data.RequestQueue;
import com.exit66.jukebox.util.XmlSegment;

public class RequestServlet extends XmlResponseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -860372254719145711L;

	protected void buildXml(HttpServletRequest req, XmlSegment xml) {
		String[] params;
		if (req.getPathInfo() != null)
			params = req.getPathInfo().split("/");
		else
			params = new String[0];
		if (params.length > 1) {
            String command = params[1];
            int result = -1;
            String mesg = "";
            
            if (command.equals("addtrack")) {
                if (params.length > 2) {
                    RequestQueue queue = new RequestQueue();
                    result = queue.requestTrack(Integer.parseInt(params[2]));
                }
            } else if (command.equals("addalbum")) {
                if (params.length > 2) {
                    RequestQueue queue = new RequestQueue();
                    result = queue.requestAlbum(Integer.parseInt(params[2]));
                }
            } else if (command.equals("addartist")) {
                if (params.length > 2) {
                    RequestQueue queue = new RequestQueue();
                    result = queue.requestArtist(Integer.parseInt(params[2]));
                }
            } else if (command.equals("empty")) {
                if (params.length > 2) {
                    RequestQueue queue = new RequestQueue();
                    queue.empty();
                }
            } else if (command.equals("remove")) {
                if (params.length > 2) {
                    String input[] = splitString(params[2], ",");
                    if (input.length >= 1) {
                        RequestQueue queue = new RequestQueue();
                        queue.removeRequests(input);
                    }
                }
            }
            
            switch (result) {
                case RequestQueue.TRACK_ALREADY_QUEUED:
                    mesg = "That track has already been requested.\nTry requesting a different song.";
                    break;
                case RequestQueue.TRACK_RECENTLY_PLAYED:
                    mesg = "That track has recently been played.\nTry requesting a different song.";
                    break;
                case RequestQueue.TRACK_REQUESTED:
                    mesg = "Thanks for the request!";
                    break;
                case RequestQueue.ALBUM_ALL_TRACKS_REQUESTED:
                case RequestQueue.ARTIST_ALL_TRACKS_REQUESTED:
                    mesg = "All the tracks have been requested.";
                    break;
                case RequestQueue.ALBUM_SOME_TRACKS_REQUESTED:
                case RequestQueue.ARTIST_SOME_TRACKS_REQUESTED:
                    mesg = "Some of the tracks have been requested.\nOthers have already been requested or have been recently played.";
                    break;
                case RequestQueue.ALBUM_NO_TRACKS_REQUESTED:
                case RequestQueue.ARTIST_NO_TRACKS_REQUESTED:
                    mesg = "None of the tracks have been requested.\nThey are already been requested or have been recently played.";
                    break;
            };
            xml.writeNode("message", mesg);
        }
	}

}
